package com.itestor.iptv.modules.common.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.itestor.iptv.modules.common.bean.RoleGroupQuery;
import com.itestor.iptv.modules.common.dao.TbRoleGroupDao;
import com.itestor.iptv.modules.common.domain.RoleGroup;
import com.itestor.iptv.modules.common.domain.RoleGroupRobot;
import com.itestor.iptv.modules.common.service.RoleGroupService;

/**
 * 
 * @author Mr.li
 * 
 */
public class RoleGroupServiceImpl implements RoleGroupService {

    private static final Logger logger = Logger
            .getLogger(RoleGroupServiceImpl.class);

    private TbRoleGroupDao tbRoleGroupDao;

    public void add(RoleGroup pGroup) {
        tbRoleGroupDao.create(pGroup);
    }

    public void delete(int[] pIds) {
        for (int id : pIds) {
            tbRoleGroupDao.delete(id);
        }
    }

    public List<RoleGroup> findAllRoleGroupList() {
        return (List<RoleGroup>) tbRoleGroupDao.findAll();
    }

    public RoleGroup get(String pRoleGroupName) {
        return tbRoleGroupDao.find(pRoleGroupName);
    }

    public RoleGroup get(int pId) {
        return tbRoleGroupDao.find(pId);
    }

    public void modify(RoleGroup pGroup) {
        tbRoleGroupDao.update(pGroup);
    }

    @SuppressWarnings("unchecked")
    public void queryRoleGroupList(RoleGroupQuery pQuery) {
        Collection<RoleGroup> grouplist = new ArrayList<RoleGroup>();

        StringBuilder hql = new StringBuilder();
        hql.append("from RoleGroup t  ");
        if (!StringUtils.isEmpty(pQuery.getRoleGroupName())) {
            hql.append(" where t.roleGroupName like '%"
                    + pQuery.getRoleGroupName() + "%'");
        }
        Query query = tbRoleGroupDao.getHibernateSession().createQuery(
                hql.toString());

        if (pQuery.getPagerCount() == -1) {
            pQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(pQuery.getPagerOffset());
        query.setMaxResults(pQuery.getPagerPerPage());

        pQuery.setQueryResults(query.list());

    }
    /**
     * 新增权限设备
     */
    public void addRelRobot(int pIdRoleGroup, List<Integer> pRobotIdList) {
        for (Integer robotId : pRobotIdList) {
            this.tbRoleGroupDao.mergeRoleGroupRobotRelation(pIdRoleGroup,
                    robotId);
        }
    }
    /**
     * 删除权限设备
     */
    public void deleteRelRobot(String pIdRoleGroup, int[] pRobotIds) {
        for (Integer robotId : pRobotIds) {
            this.tbRoleGroupDao.deleteRelRobot(Integer.parseInt(pIdRoleGroup),
                    robotId);
        }
    }
    
    /**
     * 
     * @param roleGroupId 
     * @param pIds 
     * 
     */
    public void grantExecuteRelRobot(String roleGroupId, int[] pIds){
        for (Integer robotId : pIds) {
            this.tbRoleGroupDao.updateRelRobotRoleLevel(Integer.parseInt(roleGroupId),
                    robotId,RoleGroupRobot.ROLE_LEVEL_WRITE);
            
        }
    }
    
    /**
     * 
     * @param roleGroupId 
     * @param pIds  
     */
    public void grantBrowseRelRobot(String roleGroupId, int[] pIds){
        for (Integer robotId : pIds) {
            this.tbRoleGroupDao.updateRelRobotRoleLevel(Integer.parseInt(roleGroupId),
                    robotId,RoleGroupRobot.ROLE_LEVEL_READ);
        }
    }
    
    
    /**
     * 
     * @param pTbRoleGroupDao
     */
    public void setTbRoleGroupDao(TbRoleGroupDao pTbRoleGroupDao) {
        tbRoleGroupDao = pTbRoleGroupDao;
    }

}
